package com.zhang.linkNode;

import jdk.nashorn.internal.ir.CallNode;

/**
 * 1.简单的链表
 *
 * @param <E>
 */
public class SimpleNode<E>   {
    private E value;

    /**
     * 给出个node基本类型
     * @param <E>
     * todo 标准内置单链表
     */
    class ZKNode<E> {
        public E value;
        public ZKNode next;

        ZKNode(E value) {
            this(value, null);
        }

        ZKNode(E value, ZKNode zKNode) {
            this.next = zKNode;
            this.value = value;
        }
    }
    //给个头
    private ZKNode head;
    //给个大小
    private int size;
    //给个构造
    SimpleNode() {
        head = null;
        size = 0;
    }
    /**
     * 显示链表内容
     * todo 单链表遍历
     */
    public void view() {
        ZKNode zkNode = head;
        for (int i = 0; i < size; i++) {
            System.out.println(zkNode.value);
            zkNode = zkNode.next;

        }
    }



    /**
     * 链表头部添加元素   头插法
     *
     * @param value
     */
    public void addFirst(E value) {
        this.value = value;
        ZKNode zkNode = new ZKNode(value);
        zkNode.next = this.head;
        this.head = zkNode;
        size++;
    }

    /**
     * 中间插入：给出内容和插入位置
     *
     * @param t
     * @param index
     */
    public void add(E t, int index) {
        //判断内容是否有效
        if (index < 0 || index > size) {
            throw new IllegalArgumentException("index is error");
        }
        //插入头部
        if (index == 0) {
            this.addFirst(t);
        }
        //找出目前的链表
        ZKNode preNode = this.head;
        //找到要插入节点的前一个节点
        for (int i = 0; i < index - 1; i++) {
            preNode = preNode.next;
        }
        ZKNode node = new ZKNode(t);
        //要插入的节点的下一个节点指向preNode节点的下一个节点
        node.next = preNode.next;
        //preNode的下一个节点指向要插入节点node
        preNode.next = node;
        this.size++;
    }

    /**
     * 尾插法
     * @param t
     */
    public void addLast(E t) {
        add(t, this.size);
    }

    /**
     * 删除第一个元素
     */
    public void delectFiest() {
        head = head.next;
        size--;
    }

    public void delect(int index) {
        ZKNode zkNode = head;
        if (index < size) {
            for (int i = 0; i < index; i++) {
                if (i == index - 1 & zkNode.next.next != null) {
                    zkNode.next = zkNode.next.next;
                } else if (i == index - 1 & zkNode.next.next == null) {
                    zkNode.next = null;
                } else {
                    zkNode = zkNode.next;
                }
            }
            size--;
        } else {
//            throw new IllegalArgumentException("不可到达数据");
            System.out.println("不可到达数据");
        }
    }



    /**
     * 按内容删除
     * @param value
     */
    public void delect(E value) {
        if (head == null) {
            throw new IllegalArgumentException("本链表为空");
        }
        ZKNode node = head;
        if(node.value!=null && node.value.equals(value))
        {
            node=node.next;
            size--;
            return;
        }
        while (node.next != null) {
          if (node.next.value.equals(value))
          {
              node.next=node.next.next;
              size--;
          }else {
              node=node.next;
          }
        }
    }

    public    boolean contains(E value)
    {
        ZKNode node=head;
        while (node!=null)
        {
            if (node.value.equals(value))
            {
                return true;
            }
            node=node.next;
        }
        return false;
    }

}

class Tset {
    public static void main(String[] a) {
        SimpleNode<String> stringSimplNode = new SimpleNode();
        stringSimplNode.addFirst("啊");
        stringSimplNode.addFirst("好");
        stringSimplNode.addFirst("你");
        stringSimplNode.addFirst("凯");
        stringSimplNode.addFirst("张");
        stringSimplNode.addFirst("的");
        stringSimplNode.addFirst("爱");
        stringSimplNode.addFirst("亲");
        stringSimplNode.view();
        System.out.println("-----------");
        stringSimplNode.delect(100);
        stringSimplNode.delect("啊");
        stringSimplNode.view();
        System.out.println(stringSimplNode.contains("hh"));
    }
}
